package com.wwtech.epp.ui.activity.friend;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.base.BaseActivity;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.db.Friend;
import com.wwtech.epp.entity.User;
import com.wwtech.epp.ui.activity.friend.component.DaggerUserDetailComponent;
import com.wwtech.epp.ui.activity.friend.contract.UserDetailContract;
import com.wwtech.epp.ui.activity.friend.module.UserDetailModule;
import com.wwtech.epp.ui.activity.friend.presenter.UserDetailPresenter;
import com.wwtech.epp.utils.ToastUtil;
import com.wwtech.epp.utils.WorkUtil;
import com.wwtech.epp.view.GlideCircleTransform;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongIM;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.friend
 * @Description: $description
 * @date 2017/08/31 21:45:09
 */

public class UserDetailActivity extends BaseActivity implements UserDetailContract.View {

    @Inject
    UserDetailPresenter mPresenter;
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.nick)
    TextView nick;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_position)
    TextView tvPosition;
    @BindView(R.id.tv_phone_two)
    TextView tvPhoneTwo;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_email)
    TextView tvEmail;
    @BindView(R.id.department)
    TextView department;
    private Friend freind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_user_detail);
        ButterKnife.bind(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void initData() {
        freind = getIntent().getParcelableExtra("friend");
        mPresenter.getUserInfo(freind.getUserId());
        setTitle("详细资料");
        Glide.with(this)
                .load(freind.getPortraitUri())
                .into(ivAvatar);
    }

    @Override
    protected void setupActivityComponent() {
        DaggerUserDetailComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .userDetailModule(new UserDetailModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(UserDetailContract.UserDetailContractPresenter presenter) {
        mPresenter = (UserDetailPresenter) presenter;
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public void setUserInfo(User user) {
        if (user.getRoleLevel() - ContainValue.userLogin.getData().getRoleLevel() >= 2) {
            tvPhoneTwo.setText("***********");
            tvPhone.setVisibility(View.GONE);
        } else {
            tvPhoneTwo.setText(user.getPhone());
        }
        tvName.setText(user.getNickName());
        department.setText(user.getDepartment());
        if (user.isGender()) {
            nick.setText(user.getNickName() + " (" + WorkUtil.workStatusParse(user.getWorkStatus()) + ")♂");
        } else {
            nick.setText(user.getNickName() + " (" + WorkUtil.workStatusParse(user.getWorkStatus()) + ")♀");
        }
        tvPosition.setText(user.getOrgTitle());
        tvEmail.setText(user.getEmail());
        freind.setPhone(user.getPhone());
        Glide.with(this)
                .load(user.getAvatar())
                .transform(new GlideCircleTransform(this))
                .into(ivAvatar);
        if (isFriend(user.getUserId() + "")) {

        } else {
            tvMessage.setText("加好友");
        }

    }

    @Override
    public void onAddFriendSuccess() {
        ToastUtil.displayShortToast("添加好友成功");
        EventBus.getDefault().post("success");
        tvMessage.setText("发消息");
    }

    private boolean isFriend(String userId) {
        for (Friend friend : ContainValue.friendList) {
            if (friend.getUserId().equals(userId)) {
                return true;
            }
        }
        return false;
    }

    @OnClick({R.id.tv_message, R.id.tv_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_message:
                //参数，userid 和 昵称
                if (isFriend(freind.getUserId())) {
                    RongIM.getInstance().startPrivateChat(UserDetailActivity.this, freind.getUserId(), freind.getName());
                } else {
                    OpFriend();
                }
                break;
            case R.id.tv_phone:
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + freind.getPhone()));
                startActivity(intent);
                break;
        }
    }

    private void OpFriend() {
        Map<String, String> map = new HashMap<>();
        map.put("userId", ContainValue.userLogin.getData().getUserId() + "");
        map.put("targetId", freind.getUserId());
        map.put("op", "0");         //0添加,1删除
        mPresenter.opFriend(map);
    }
}